package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ThreadServidor extends Thread {

    private Socket s1;

    public ThreadServidor(Socket s1) {
        this.s1 = s1;
    }

    private long fibo(int n) {
        if (n < 2) {
            return 1L;
        }
        long fn1 = 1;
        long fn2 = 1;
        long atual = 0;
        for (int i = 2; i <= n; i++) {
            atual = fn1 + fn2;
            fn2 = fn1;
            fn1 = atual;
        }
        return atual;
    }

    private long fiboRecursiva(int n){
        if(n<2)return 1L;
        else return fiboRecursiva(n-1)+ fiboRecursiva(n-2);
    }
    @Override
    public void run() {
        try {
            BufferedReader entrada = new BufferedReader(new InputStreamReader(s1.getInputStream()));
            PrintWriter saida = new PrintWriter(new OutputStreamWriter(s1.getOutputStream()));
            while (true) {
                Integer i1 = new Integer(entrada.readLine());
                if (i1 == -1) {
                    break;
                }
                saida.println(fibo(i1));
                saida.flush();
            }
            s1.close();
        } catch (IOException ex) {
            Logger.getLogger(ThreadServidor.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
